{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "test.txt\n",
      "False\n",
      "w\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "fo = open('test.txt','w')\n",
    "print fo.name\n",
    "print fo.closed\n",
    "print fo.mode\n",
    "print fo.softspace\n",
    "\n",
    "fo.write('智障儿童欢乐多')\n",
    "\n",
    "fo.close()\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "智障\n",
      "儿童\n",
      "智障儿童\n"
     ]
    }
   ],
   "source": [
    "# -*-coding:utf-8-*-\n",
    "\n",
    "fo1 = open('test.txt','r+')\n",
    "\n",
    "# 一个汉字占3个字符\n",
    "str = fo1.read(6)\n",
    "print str\n",
    "\n",
    "str1 = fo1.read(6)\n",
    "print str1\n",
    "\n",
    "fo1.seek(0,0)\n",
    "str2 = fo1.read(12)\n",
    "print str2\n",
    "\n",
    "fo1.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Chan Kiang\\Documents\\Python Scripts\\python_exercise\\module0129\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "\n",
    "print os.getcwd()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "内容写入文件成功\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    #fo3 = open('test.txt','r')\n",
    "    #fo3 = open('test.txt','w')\n",
    "    fo3 = open('test.txt','w+')\n",
    "    fo3.write('这是一个测试 用来测试异常 aaa') \n",
    "    print \n",
    "except IOError:\n",
    "    print '没有找到文件或读取文件失败'\n",
    "    fo3.close()\n",
    "else:\n",
    "    print '内容写入文件成功'\n",
    "    fo3.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Networkerror(RuntimeError):\n",
    "    def __init__(self,arg):\n",
    "        self.args = arg\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('B', 'a', 'd', ' ', 'h', 'o', 's', 't', 'n', 'a', 'm', 'e')\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    raise Networkerror(\"Bad hostname\")\n",
    "except Networkerror,e:\n",
    "    print e.args"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
